package com.guoqi.keepsign.mzt

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentStatePagerAdapter
import android.support.v4.content.ContextCompat
import android.support.v4.view.PagerAdapter
import android.support.v4.view.ViewPager
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import com.esh.oa.search.SEARCH_TAG
import com.esh.oa.search.SearchFragment
import com.guoqi.keepsign.IPConfig
import com.guoqi.keepsign.R
import kotlinx.android.synthetic.main.ui_bxs_tab.*
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.UIUtil
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView
import java.util.*

class MztTabUI : AppCompatActivity() {
    private lateinit var newFragment: MztFm
    private lateinit var hotFragment: MztFm
    private lateinit var recommendFragment: MztFm
    private lateinit var zhuantiFragment: MztFm
    private lateinit var xingganFragment: MztFm
    private lateinit var mmFragment: MztFm
    private lateinit var zipaiFragment: MztFm
    private lateinit var jiepaiFragment: MztFm
    private lateinit var japanFragment: MztFm
    private lateinit var taiwanFragment: MztFm
    private lateinit var searchFragment: SearchFragment

    private val tabList = Arrays.asList("最新", "最热", "推荐", "性感", "清纯", "日本", "台湾", "自拍", "街拍", "专题")
    private lateinit var mAdapter: PagerAdapter
    private var pageViews = ArrayList<Fragment>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.ui_bxs_tab)

        //隐藏nav
//        val decorView = window.decorView
//        val option = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//        decorView.systemUiVisibility = option
//        window.navigationBarColor = Color.TRANSPARENT

        //隐藏底栏
//        val params = window.attributes
//        params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
//        window.attributes = params

        //初始化页面
        initViewPager()
        initIndicator()
    }

    private fun initViewPager() {
        newFragment = MztFm.newInstance(IPConfig.MZT_NEW, 2)
        hotFragment = MztFm.newInstance(IPConfig.MZT_HOT, 2)
        recommendFragment = MztFm.newInstance(IPConfig.MZT_RECOMMEND, 2)
        xingganFragment = MztFm.newInstance(IPConfig.MZT_XINGGAN, 2)
        mmFragment = MztFm.newInstance(IPConfig.MZT_MM, 2)
        japanFragment = MztFm.newInstance(IPConfig.MZT_JAPAN, 2)
        taiwanFragment = MztFm.newInstance(IPConfig.MZT_TAIWAN, 2)
        zipaiFragment = MztFm.newInstance(IPConfig.MZT_ZIPAI, 1)
        jiepaiFragment = MztFm.newInstance(IPConfig.MZT_JIEPAI, 1)
        zhuantiFragment = MztFm.newInstance(IPConfig.MZT_ZHUANTI, 3)
        pageViews.add(newFragment)
        pageViews.add(hotFragment)
        pageViews.add(recommendFragment)
        pageViews.add(xingganFragment)
        pageViews.add(mmFragment)
        pageViews.add(japanFragment)
        pageViews.add(taiwanFragment)
        pageViews.add(zipaiFragment)
        pageViews.add(jiepaiFragment)
        pageViews.add(zhuantiFragment)
        setViewPagerAdapter()
    }

    private fun initIndicator() {
        iv_search.visibility = View.VISIBLE
        iv_search.setOnClickListener {
            searchFragment = SearchFragment()
            searchFragment.show(fragmentManager, SEARCH_TAG)
        }
        val commonNavigator = CommonNavigator(this)
        commonNavigator.scrollPivotX = 0.35f
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getCount(): Int {
                return tabList?.size ?: 0
            }

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val tv_tab = SimplePagerTitleView(context)
                tv_tab.text = tabList[index]
                tv_tab.textSize = 16f
                tv_tab.paint.isFakeBoldText = true
                tv_tab.normalColor = Color.parseColor("#9e9e9e")
                tv_tab.selectedColor = ContextCompat.getColor(this@MztTabUI, R.color.colorAccent)
                tv_tab.setOnClickListener { viewPager.currentItem = index }
                return tv_tab
            }

            override fun getIndicator(context: Context): IPagerIndicator {
                val indicator = LinePagerIndicator(context)
                indicator.mode = LinePagerIndicator.MODE_WRAP_CONTENT
                indicator.lineHeight = UIUtil.dip2px(context, 3.0).toFloat()
                indicator.lineWidth = UIUtil.dip2px(context, 20.0).toFloat()
                indicator.roundRadius = UIUtil.dip2px(context, 2.0).toFloat()
                indicator.startInterpolator = AccelerateInterpolator()
                indicator.endInterpolator = DecelerateInterpolator(2.0f)
                indicator.setColors(ContextCompat.getColor(this@MztTabUI, R.color.colorAccent))
                return indicator
            }
        }
        magicIndicator.navigator = commonNavigator
        ViewPagerHelper.bind(magicIndicator, viewPager)
    }

    private fun setViewPagerAdapter() {
        mAdapter = object : FragmentStatePagerAdapter(supportFragmentManager) {

            override fun getItem(arg0: Int): Fragment {
                return pageViews[arg0]
            }

            override fun getCount(): Int {
                return pageViews.size
            }
        }

        viewPager.adapter = mAdapter
        viewPager.currentItem = 0
        viewPager.offscreenPageLimit = 1
        viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {

            override fun onPageSelected(i: Int) {
                magicIndicator.onPageSelected(i)
            }

            override fun onPageScrolled(i: Int, arg1: Float, arg2: Int) {
                magicIndicator.onPageScrolled(i, arg1, arg2)
            }

            override fun onPageScrollStateChanged(arg0: Int) {
                magicIndicator.onPageScrollStateChanged(arg0)
            }
        })
    }


//    override fun onWindowFocusChanged(hasFocus: Boolean) {
//        super.onWindowFocusChanged(hasFocus)
//        if (hasFocus) {
//            val decorView = window.decorView
//            decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
//                    or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//                    or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                    or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
//                    or View.SYSTEM_UI_FLAG_FULLSCREEN
//                    or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
//        }
//    }
}

//    private class MyHandler(activity: SplashActivity) : Handler() {
//        private val mActivity: WeakReference<SplashActivity> = WeakReference(activity)
//
//        override fun handleMessage(msg: Message) {
//            if (mActivity.get() == null) {
//                return
//            }
//            when (msg.what) {
//                SWITCH_MAINACTIVITY -> {
//                    Log.e(TAG, "SplashActivity --- switch_MainActivity")
//                }
//                else -> {
//                }
//            }
//        }
//    }